
from XmlData2DictData import Xml2Dict
from const_data import *
from nd_utils.str_util import get_name
class Answer_extration():

    def __init__(self, xml2dict):
        self.xml2dict = xml2dict

        self.publication_dict = self.xml2dict.publication_dict
        self.paperidx_list = self.xml2dict.paperidx_list
        self.paperid_idx_dict = self.xml2dict.paperid_idx_dict

        self.label_paperidx_list_dict = self._get_label_paperidx_list_dict()

        self.true_paperidx_cluster_list = self.label_paperidx_list_dict.values()

        self.true_cluster_num = len( self.true_paperidx_cluster_list )
    """
        idx :  coauthor list
    """
    def _get_label_paperidx_list_dict(self):
        label_paperidx_list_dict = {}

        for paperid, publication_attr_dict in self.publication_dict.items():
            label = publication_attr_dict['label']

            if label not in label_paperidx_list_dict:
                label_paperidx_list_dict[label] = [ self.paperid_idx_dict[paperid] ]
            else:
                label_paperidx_list_dict[label].append( self.paperid_idx_dict[paperid] )
        return label_paperidx_list_dict



def tezt_Answer_extration():
    xml_file_name_list = [
        xml_file_name7,

        xml_file_name1,
        xml_file_name2,
        xml_file_name3,
        xml_file_name4,
        xml_file_name5,
        xml_file_name6,
        xml_file_name8,
        xml_file_name9,
        xml_file_name10
    ]
    # for xml_file_name in xml_file_name_list:
    #     print_label_paperidx_list_xml(xml_file_name)
    #     print "*****************************************"
    print_label_paperidx_list_xml(xml_file_name7)
def print_label_paperidx_list_xml(xml_file_name):
    xml2dict = Xml2Dict(xml_dir + xml_file_name)
    answer_extration = Answer_extration(xml2dict)
    for paperidx_list in answer_extration.label_paperidx_list_dict.values():
        print paperidx_list,  len(paperidx_list)
    # print answer_extration.true_paperidx_cluster_list


def print_dataset_static():
    for xml_file_name in xml_experiment_file_name_list:
        xml_filepath = xml_dir + xml_file_name
        xml2dict = Xml2Dict(xml_filepath)
        answer_extration = Answer_extration(xml2dict)
        print get_name(xml_file_name), len(answer_extration.publication_dict.keys()), answer_extration.true_cluster_num

from nd_utils.write_csv import write_csv_f

def write_dataset_static():
    new_csv_row_list = []
    for xml_file_name in xml_experiment_file_name_list:
        new_csv_row = []
        xml_filepath = xml_dir + xml_file_name
        xml2dict = Xml2Dict(xml_filepath)
        answer_extration = Answer_extration(xml2dict)
        print get_name(xml_file_name), len(answer_extration.publication_dict.keys()), answer_extration.true_cluster_num
        new_csv_row.append( str( get_name(xml_file_name) ) )
        new_csv_row.append( str(  len(answer_extration.publication_dict.keys()) ) )
        new_csv_row.append( str( answer_extration.true_cluster_num ) )
        new_csv_row_list.append( new_csv_row )
    write_csv_f('dataset_static', new_csv_row_list, fileHeader2)

if __name__ == '__main__':
    # tezt_Answer_extration()
    # print_dataset_static()
    write_dataset_static()